package com.lzw.dao;

import com.lzw.pojo.Student;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface StudentDao {
    //学生全查(包含学生所在的班级信息)
    @Results(id="stuMap",value={
            @Result(id =true,property ="stuNo",column = "stuNo"),
            @Result(property ="stuName",column = "stuName"),
            @Result(property ="stuAge",column = "stuAge"),
            @Result(property = "cla",column = "cid",
            one = @One(select = "com.lzw.dao.ClassDao.getClassById"))
    })
    @Select("select * from student ")
    List<Student> getAllStu();
    //删除选中的功能:
    @Delete("delete from student where stuNo in (${ids})")
    int delAll(@Param("ids") String ids);
    //学生信息的添加
    @Insert("insert into student(stuName,stuAge,cid) values (#{stuName},#{stuAge},#{cla.cid})")
    int addStu(Student student);
    //编辑学生信息
    //先查后改
    @ResultMap("stuMap")
    @Select("select * from student where stuNo=#{stuNo}")
    Student getStuByNo(Integer stuNo);
    @Update("update student set stuName=#{stuName},stuAge=#{stuAge},cid=#{cla.cid} where stuNo=#{stuNo}")
    int updateStu(Student stu);
    //分页显示功能
    //1 统计总数量
    @Select("<script>select count(*) from student <where>"+
           "<if test='stuNo!=null'>stuNo=#{stuNo}</if>" +
           "<if test=\"stuName!=null and stuName.trim()!=''\"> and stuName like concat('%',#{stuName},'%')</if>"+
           "<if test='cid!=null and cid!=-1'> and cid=#{cid}</if>"+
           "</where></script>")
    Integer getStuCount(Map<String,Object> map);
    //2 分页显示
    @ResultMap("stuMap")
    @Select(" <script> select * from student <where>" +
            "<if test='stuNo!=null'>stuNo=#{stuNo}</if>" +
            "<if test=\"stuName!=null and stuName.trim()!=''\"> and stuName like concat('%',#{stuName},'%')</if>" +
            "<if test='cid!=null and cid!=-1'> and cid=#{cid}</if>" +
            "</where> limit #{index},#{pageSize} </script>")
    List<Student> getStuPage(Map<String,Object> map);
    //删除单个学生的功能
    @Delete("delete from student where stuNo=#{stuNo}")
    Integer delStu(int stuNo);
}

